<!--ID:distribution-->
<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../../libs/mui.min.css" rel="stylesheet"/>
    <link href="../../css/main.css" rel="stylesheet"/>
    <link rel="stylesheet" href="../../css/mine-distribution.css"/>
</head>

<body>
<div class="head">
    <img class="mui-action-back" src="../../images/all-left.png" alt="">
    <p>我的分销中心</p>
</div>

<div class="content">
    <div class="content-top">
        <p class="content-top-money">本月收入(元)</p>
        <p class="content-top-num">0.00</p>
        <p class="content-top-all">累计收入 ¥ <span>0.00</span></p>
    </div>

    <div class="content-center">
        <p class="content-center-ti">
            <span id="in-record" class="active">收入记录</span><span id="my-team">我的团队</span>
        </p>
    </div>
    <button class="bindAlipay" id="bindAlipay">绑定支付宝</button>
</div>
<!--切换展示区-->
<div class="tab">
    <div class="tab-le actives">
        <!--<div class="tab-le-fi">
            <p>
                <span>代理商编号：2290</span>
                <span class="tab-le-fi-add">+500</span>
            </p>
            <p>
                <span>2016年11月1日</span>
                <span class="tab-le-fi-ri">直接收入</span>
            </p>
        </div>-->
    </div>
    <div class="tab-ri">
        <!--<div class="tab-ri-fi">
            <div class="tab-ri-fi1">
                <p>哈哈</p>
                <p class="userInfo">银行经理</p>
            </div>
            <div class="tab-ri-fi2">
                <p>15625545652</p>
            </div>
        </div>-->
    </div>
</div>

<script src="../../js/jquery-3.1.1.min.js"></script>
<script src="../../libs/mui.min.js"></script>
<script src="../../js/url.js"></script>
<script>
    $('#bindAlipay').on('tap', function () { //绑定支付宝跳转
        href('bind-alipay.html', 'bind.alipay')
    })
    //我的团队
    $("#my-team").on('tap', function () {
        $.ajax({
            url: ur + 'rest/distributorController/subordinateList',
            type: 'get', headers: {"X-Token": localStorage.token}, dataType: 'json',
            success: function (data) {
                if (data.meta.code == "S002") {
                    var msg = data.data.list;
                    if (data.data == null || msg.length === 0) {
                        mui.toast('您还没有团队.');
                    } else {
                        var itemL = $('.tab-ri-fi').length;
                        var UserInfo = ['', '银行经理', '贷款中介'];
                        for (var i = msg.length - 1; i >= itemL; i--) {
                            var teamData = '<div class="tab-ri-fi" userId="' + msg[i].id + '">' +
                                '<div class="tab-ri-fi1">' +
                                '<p>' + msg[i].nickName + '</p></div>' +
                                '<div class="tab-ri-fi2">' +
                                '<p><span>' + UserInfo[msg[i].type] + '</span>' + msg[i].phone + '</p>' +
                                '</div></div>';
                            $('.tab-ri').append(teamData);
                        }
                    }
                } else {
                    // console.log(data.meta.code);
                }
            }
        });
    });
    // 查看二级成员
    $(document).on('tap', '.tab-ri-fi', function () {
        var $this = $(this);
        var userId = $this.attr('userId');
        if (!$this.hasClass('tap')) {
            $this.find('.next').remove();
            $.ajax({
                url: ur + 'rest/distributorController/subordinateList',
                type: 'get', headers: {"X-Token": localStorage.token}, dataType: 'json',
                data: {userId: userId},
                success: function (data) {
                    if (data.meta.code == "S002") {
                        var msg = data.data.list;
                        if (data.data == null || msg.length === 0) {
                            mui.toast('您还没有二级团队.');
                        } else {
                            for (var i = msg.length - 1; i >= 0; i--) {
                                var UserInfo = ['', '银行经理', '贷款中介'];
                                var teamData = '<div class="next" style="display: none">' +
                                    '<div class="next1"><p>' + msg[i].nickName + '</p></div>' +
                                    '<div class="next2"><p>' +
                                    '<span>' + UserInfo[msg[i].type] + '</span>' + msg[i].phone + '</p></div>' +
                                    '</div>'
                                $this.append(teamData);
                                $this.find('.next').slideDown('300');
                            }
                        }
                        $this.addClass('tap');
                    } else {
                        mui.toast(data.meta.code);
                    }
                }
            });
        } else {
            $this.find('.next').slideUp('300');
            $this.removeClass('tap');
        }
    });

    function getTime(end) { //时间
        var time = new Date();
        var y = time.getFullYear();
        var m = (time.getMonth() + 1 > 10) ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1));
        var d = (time.getDate() + end > 10) ? (time.getDate() + end) : ('0' + (time.getDate() + end));
        return y + "-" + m + "-" + d;
    }

    //收入记录
    function incomeRecord() {
        $.ajax({
            type: "get",
            url: ur + "rest/commissionController/detailList",
            async: true,
            dataType: "json",
//            data: { // 备用参数 目前没这个需求
//                startTime: getTime(0),
//                endTime: getTime(1),
//            },
            headers: {
                "X-Token": localStorage.token
            },
            success: function (data) {
                console.log('收入记录', data);
                // plus.nativeUI.closeWaiting();
                Date.prototype.toLocaleString = function () {
                    return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + '日';
                };
                if (data.meta.code === 'S002') {
                    var listData = data.data.list;
                    var n = data.data.list.length;
                    var itemL = $('.tab-le-fi').length;
                    for (var i = n - 1; i >= itemL; i--) {
                        var inAdd = '<div class="tab-le-fi">' +
                            '<p><span>' + listData[i].partnerName + '</span><span class="tab-le-fi-add">+' +
                            listData[i].money + '</span></p>' +
                            '<p><span>' + new Date(listData[i].time).toLocaleString() +
                            '</span><span class="tab-le-fi-ri">直接收入</span></p>' +
                            '</div>';
                        $('.tab-le').append(inAdd)
                    }
                } else {
                    mui.toast(data.meta.message);
                }
            },
        });
    }
    incomeRecord();
    $('#in-record').on('tap', incomeRecord);

    //汇总
    $.ajax({
        type: "get",
        url: ur + "rest/commissionController/sumList",
        async: true,
        headers: {
            "X-Token": localStorage.token
        },
        success: function (data) {
            var data = JSON.parse(data);
            //如果为null则为0；
            for (var key in data.data) {
                if (data.data[key] == null) {
                    data.data[key] = 0;
                }
            }
            if (data.data.allincome != null && data.data.monthincome != null) {
                $(".content-top-all span").text(data.data.allincome);
                $('.content-top-num').text(data.data.monthincome)
            }
        }
    });
    //切换
    $('.content-center-ti>span').on('tap', function () {
        $(this).attr('class', 'active');
        $(this).siblings().removeClass('active');
        $('.tab>div').eq($(this).index()).addClass('actives');
        $('.tab>div').eq($(this).index()).siblings().removeClass('actives');
    });

    // 绑定支付宝
    $('.bindAlipay').on('tap', function () {
        $.ajax({
            url: ur + 'alipayController/save',
            headers: {'X-Token': localStorage.token},
            type: 'POST',
            dataType: 'json',
            data: {
                email: '',
                id: '', // id为空则为新增，更新则使用获取绑定信息中的id
                realName: '' // 真实姓名
            }
        }).done(function (res) {
//					console.log(res)
        })
    });
</script>
</body>

</html>